package com.wujilee.wujileenetty.demos.web.netty.heima;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author liwuji
 * @date 2024/8/9
 * @desc
 **/
@Slf4j
public class TestPipneLine {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
//                        ch.pipeline().addLast(new StringDecoder());
                        log.info("服务端接收到了新的连接");
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("h1", new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        System.out.println("1");
                                        super.channelRead(ctx, msg);
                                    }
                                }
                        );

                        pipeline.addLast("h2", new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        System.out.println("2");
                                        super.channelRead(ctx, msg);

                                    }
                                }
                        );
                        pipeline.addLast("h3", new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        System.out.println("3");
                                        Channel channel = ctx.channel();
                                        channel.writeAndFlush(ctx.alloc().buffer().writeBytes("hello service".getBytes()));
                                        super.channelRead(ctx, msg);

                                    }
                                }
                        );

                        pipeline.addLast("h4", new ChannelOutboundHandlerAdapter() {
                                    @Override
                                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                        System.out.println("4");
                                        super.write(ctx, msg, promise);
                                    }
                                }
                        );
                        pipeline.addLast("h5", new ChannelOutboundHandlerAdapter() {
                                    @Override
                                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                        System.out.println("5");
                                        super.write(ctx, msg, promise);
                                    }
                                }
                        );
                    }
                })
                .bind(6666);
    }

}

